#Region " Imports "

Imports BlazeApps.Library.Editor
Imports BlazeApps.Library.Page
Imports System.Web.Security
Imports AjaxControlToolkit
Imports BlazeApps.Library.Options

#End Region

Partial Class Usercontrols_Viewer
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Protected Property PageRevision() As Integer
        Get
            Return ViewState("PageRevision")
        End Get
        Set(ByVal value As Integer)
            ViewState.Add("PageRevision", value)
        End Set
    End Property

    Protected Property PagePreviousRevision() As Integer
        Get
            Return ViewState("PagePreviousRevision")
        End Get
        Set(ByVal value As Integer)
            ViewState.Add("PagePreviousRevision", value)
        End Set
    End Property

    Protected Property PageGuid() As Guid
        Get
            Return GetGuidFromVS("PageGuid")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("PageGuid", value)
        End Set
    End Property

    Protected Property PageViewed() As Boolean
        Get
            Return GetBooleanFromSession("PageViewed")
        End Get
        Set(ByVal value As Boolean)
            Session.Add("PageViewed", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        GetQueryStringValues()

        If Me.Page.IsPostBack = False Then
            SetViewed(PageGuid, False)
            GetViewerDetail()
            GetPageDetails()
            If IsPrintMeOrPopUpActive() = False Then
                LoadOptions()
            End If
        End If

		SetPageState()

        Me.uxViewerPanel.Visible = IsAllowedItemAccess(PageGuid)

    End Sub

#End Region

#Region " Save Data "

#End Region

#Region " Load Data "

    Private Sub LoadOptions()
        Dim Items As OptionDetail = OptionDetail.GetOptions(PageGuid)

        'Hide/Show Panels based on page settings
        Me.uxSummaryPanel.Visible = Items.AllowSummary
        Me.uxFavoritesPanel.Visible = (Items.AllowSubscriptions And EnableFavorites)
        Me.uxRaingPanel.Visible = (Items.AllowRanking And EnableRating)
        Me.uxCommentsPanel.Visible = (Items.AllowComments And EnableComments)
        Me.uxTagsPanel.Visible = (Items.AllowTags And EnableTags)
        Me.uxToolsPanel.Visible = (Items.AllowInfoTools And EnableTools)

        'Enable/Disable Panels based on page settings
        Me.uxSummaryPanel.Enabled = Items.AllowSummary
        Me.uxFavoritesPanel.Enabled = (Items.AllowSubscriptions And EnableFavorites)
        Me.uxRaingPanel.Enabled = (Items.AllowRanking And EnableRating)
        Me.uxCommentsPanel.Enabled = (Items.AllowComments And EnableComments)
        Me.uxTagsPanel.Enabled = (Items.AllowTags And EnableTags)
        Me.uxToolsPanel.Enabled = (Items.AllowInfoTools And EnableTools)

    End Sub

    Public Sub GetPageDetails()
        Dim PageDetails As PageDetail = PageDetail.GetPageDetail(PageGuid)

        SetPageTitle(PageDetails.title)
        Me.uxPageTitleLabel.Text = PageDetails.title
        Me.uxSummaryDetailLabel.Text = PageDetails.summary

        PageRevision = PageDetails.revision

        'Me.uxTools.PageID = PageDetails.kbid
        Me.uxTools.PageRevision = PageDetails.revision
        Me.uxTools.EditedDate = PageDetails.statusdateString
        'Me.uxTools.PageViews = PageDetails.Views
        'Me.uxTools.PageComments = PageDetails.Comments
        Me.uxRating.Revision = PageDetails.revision
        Me.uxFavorites.FavoriteType = BlazeApps.Library.Base.ItemType.Pages
        Me.uxPageTags.TagType = BlazeApps.Library.Base.ItemType.Pages

        AddMetaTags(PageDetails.keywords, PageDetails.summary)

    End Sub

    Public Function GetViewerDetail() As Boolean

        Dim List As New PageBody

        If GetIntegerFromQueryString("revision") > 0 Then
            With Me.uxPageRepeater
                .DataSource = List.GetPageBodyByRevision(PageGuid, PagePreviousRevision)
                .DataBind()
            End With
        ElseIf GetBooleanFromQueryString("preview") = True Then
            With Me.uxPageRepeater
                .DataSource = List.GetPageBodyPreview(PageGuid)
                .DataBind()
            End With
        Else
            With Me.uxPageRepeater
                .DataSource = List.GetPageBody(PageGuid)
                .DataBind()
            End With
        End If

    End Function

#End Region

#Region " Form Controls Events "

	Protected Sub uxEditButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxEditButton.Click
		Response.Redirect(String.Format("{0}.aspx?pid=editor&id={1}", AppPage, New String(PageGuid.ToString)))
	End Sub

	Protected Sub uxManageButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxManageButton.Click
		Response.Redirect(String.Format("{0}.aspx?pid=management&id={1}", AppPage, New String(PageGuid.ToString)))
	End Sub

	Protected Sub uxCheckInOutButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxCheckInOutButton.Click
		If IsCheckedOutByCurrentUser(PageGuid, CurrentUserID) = True Then
			CheckIn(PageGuid, CurrentUserID)
		Else
			CheckOut(PageGuid, CurrentUserID)
		End If

		SetEditControlsState()
	End Sub

	Protected Sub uxPageRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles uxPageRepeater.ItemDataBound
		Dim ctl As Control = e.Item.FindControl("uxPageContainer")
		Dim lit As Literal = ctl.Controls.Item(0).FindControl("uxPageTitleLiteral")
		Dim lbl As Label = ctl.Controls.Item(0).FindControl("uxPageViewDataLabel")

		'CPE.CollapsedImage = String.Format("~/App_Themes/{0}/images/icons/collapse.gif", Page.Theme.ToString.Trim)
		'CPE.ExpandedImage = String.Format("~/App_Themes/{0}/images/icons/expand.gif", Page.Theme.ToString.Trim)

		If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
			If ShowItem(ConvertToGuid(e.Item.DataItem("templatefieldid").ToString)) = True Then
				lit.Text = e.Item.DataItem("fieldname").ToString
				If IsDBNull(e.Item.DataItem("showname")) Then
					lit.Visible = True
				Else
					lit.Visible = CType(e.Item.DataItem("showname"), Boolean)
				End If
				lbl.Text = HighlightText(GetStringFromSession("searchtext"), e.Item.DataItem("placeholderdata").ToString)
			Else
				e.Item.Visible = False
			End If
		End If

	End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        PageGuid = GetGuidFromQueryString("id")
        PagePreviousRevision = GetIntegerFromQueryString("revision")
    End Sub

    Private Sub SetPageState()

        If IsPrintMeOrPopUpActive() = True Then
            If GetBooleanFromQueryString("printme") = True Then
                Me.uxPrintMePanel.Visible = True
                Me.uxPrintMePanel.Enabled = True
            End If
            Me.uxCommentsList.Visible = False
            Me.uxCommentsList.HideData = True
            Me.uxTools.Visible = False
            Me.uxTools.HideData = True
            Me.uxRating.Visible = False
            Me.uxRating.HideData = True
            Me.uxFavorites.Visible = False
            Me.uxFavorites.HideData = True
            Me.uxPageTags.Visible = False
            Me.uxPageTags.HideData = True
		End If

		Me.uxPageEditingPanel.Visible = IsEditor()
		Me.uxPageEditingPanel.Enabled = IsEditor()

		If IsUserAuthenticated() Then
			SetEditControlsState()
		End If

	End Sub

	Private Sub SetEditControlsState()
		If Not PageGuid = Guid.Empty Then
			If IsCheckedOut(PageGuid) = False Then
				Me.uxCheckInOutButton.Text = Resources.Resource.CheckOut
			Else
				If IsCheckedOut(PageGuid) = True AndAlso IsCheckedOutByCurrentUser(PageGuid, CurrentUserID) = True Then
					Me.uxCheckInOutButton.Text = Resources.Resource.CheckIn
				ElseIf IsCheckedOut(PageGuid) = True AndAlso IsCheckedOutByCurrentUser(PageGuid, CurrentUserID) = False Then
					DisableAll(Me.Page)
					Me.uxCheckInOutButton.Text = Resources.Resource.Locked
					Me.uxEditButton.Enabled = False
					Me.uxManageButton.Enabled = False
					Me.uxLockedByLabel.Text = "The page is currently checked out to another user"
				End If
			End If
		Else
			Me.uxCheckInOutButton.Visible = False
		End If
	End Sub

#End Region

End Class
